package com.example.fill;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Scroller;

import androidx.annotation.Nullable;

import com.example.fill.classbean.ClassBean;

import java.util.ArrayList;
import java.util.List;

public class ClassView extends View {
    private Scroller scroller;
    private final List<List<List<ClassBean>>> clazz = new ArrayList<>();

    public ClassView(Context context) {
        super(context);
        init();
    }

    public ClassView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ClassView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private Paint line = new Paint();

    private void init() {
        scroller = new Scroller(getContext());
        line.setColor(Color.WHITE);
        line.setStrokeWidth(2f);
        line.setStyle(Paint.Style.FILL);
    }

    private int mHeight;
    private int mWidth;
    private int mPageWidth;

    int classHeight = 0;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        // 获取宽-测量规则的模式和大小
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        mPageWidth = MeasureSpec.getSize(widthMeasureSpec);

        // 获取高-测量规则的模式和大小
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        mHeight = MeasureSpec.getSize(heightMeasureSpec);

        mWidth = mPageWidth * clazz.size();
        setMeasuredDimension(mWidth,mHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        for (int i = 0; i < clazz.size(); i++) {
            canvas.drawLine(0,0,mWidth,0,line);
        }
    }


    /**
     * 处理屏幕滑动事件
     */
    private int downX;
    private int downY;

    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = (int) event.getX();//按下时的地方
                downY = (int) event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                Log.i("TAG", "---------------onTouchEvent------------------ ");

                int moveX = (int) event.getX();//当前滑动的位置
                int moveY = (int) event.getY();
                if (Math.abs(moveX - downX) < Math.abs(moveY - downY))
                    return super.onTouchEvent(event);
                Log.i("TAG", "moveX:" + moveX);
                int deltaX = moveX - downX;//与上一次滑动的距离（这个downX在case最后被赋值了，别被名字骗了
                Log.i("TAG", "deltaX:" + deltaX);
                int newScrollX = getScrollX() - deltaX;//上一次的滑动x距离和此次的滑动距离差（即真正需要滑动的距离
                Log.i("TAG", "newScrollX:" + newScrollX);
                if (newScrollX > 0) newScrollX = 0;//如果你是往右边扒拉，对不起，不动
                Log.i("TAG", "newScrollX:" + newScrollX);
                scrollTo(newScrollX, 0);//开划
                downX = moveX;
                break;
            case MotionEvent.ACTION_UP:
                //当滑动距离小于Menu宽度的一半时，平滑滑动到主页面
                if (getScrollX() > -menuWidth / 2) {
                    closeMenu();
                } else {
                    //当滑动距离大于Menu宽度的一半时，平滑滑动到Menu页面
                    openMenu();
                }
                break;
        }
        return true;
    }

    //关闭menu
    private void closeMenu() {
        scroller.startScroll(getScrollX(), 0, 0 - getScrollX(), 0, 400);
        invalidate();
    }

    //打开menu
    private void openMenu() {
        scroller.startScroll(getScrollX(), 0, -menuWidth - getScrollX(), 0, 400);
        invalidate();
    }

}
